<?php
	function f500($sql){
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("500");

		$dataType = getValue($_GET['dataType']);
		$action = getValue($_GET['action']);

		if ($action == "getItems"){
			$format = array(
				"time_stamp" => "dateY.m.d",
				"time" => "dateH:i:s",
			);

			$dataHandle->setFormat($format);

			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "sys_Events","time_stamp");
				$query = getJSONQuery($startTime, $endTime, $filter);
				//echo $query;
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else if($action == "getYears"){
			$query = getYears();
			print($dataHandle->printJSON($sql, $query));
		}
		else{
			$temp = array();
			$temp['error'] = "500: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getJSONQuery(&$startTime, &$endTime, &$filter){
		$queryString = "
			SELECT
				time_stamp,
				time_stamp as time,
				entity_name,
				entity_no,
				action,
				message
			FROM sys_Events";

		$queryString = $queryString.$filter;

		$oderByFiled = getValue($_GET['sortItem']);
		$oderByValue = getValue($_GET['ascDesc']);

		if($oderByFiled !== "" and $oderByValue !== "")
			$queryString = $queryString." order by ".$oderByFiled." ".$oderByValue;

		return $queryString;
	}
	function getYears(){
		$queryString = 
			"SELECT DISTINCT 
				DATEPART(year, time_stamp) as availableYear 
			FROM sys_Events 
			order by availableYear desc";
		return $queryString;
	}
?>